package com.smfx.yxt.controller.teacher;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itextpdf.text.pdf.PdfStructTreeController.returnType;
import com.smfx.yxt.common.AppUser;
import com.smfx.yxt.common.Const;
import com.smfx.yxt.common.MenuConst;
import com.smfx.yxt.common.RspResult;
import com.smfx.yxt.common.enums.RelationType;
import com.smfx.yxt.common.enums.Status;
import com.smfx.yxt.controller.BaseController;
import com.smfx.yxt.model.Grid;
import com.smfx.yxt.model.PrincipalMailbox;
import com.smfx.yxt.model.TeacherHome;
import com.smfx.yxt.page.OrderBy;
import com.smfx.yxt.page.Page;
import com.smfx.yxt.reqVo.MailboxReqVO;
import com.smfx.yxt.service.IOrganizeGroupService;
import com.smfx.yxt.service.IPrincipalMailboxService;
import com.smfx.yxt.service.ITeacherHomeService;
import com.smfx.yxt.view.PrincipalMailboxView;
import com.smfx.yxt.view.TeacherHomeView;
import com.smfx.yxt.vo.PrincipalMailboxQueryVO;
import com.smfx.yxt.vo.TeacherHomeQueryVO;

@RequestMapping(Const.TEACHER_ROLE + MenuConst.TEACHER_HOME)
@Controller
public class TeacherHomeController extends BaseController {
	
	private Logger logger = LoggerFactory.getLogger(TeacherHomeController.class);
	
	@Autowired
	private ITeacherHomeService teacherHomeService;
	@Autowired
	private IOrganizeGroupService organizeGroupService;
	
	@RequestMapping(value = {"","/"})
	public String index(Model model){
		AppUser user = getAppUser();
		int count = organizeGroupService.isGroupTeacher("TEACHER_HOME", user.getTeacher().getId());
		if(count > 0) {
			return "teacherHome/list";
		}
		model.addAttribute("relationType", RelationType.TEACHER_HOME.getCode());
		return "teacherHome/publish";
	}
	
	@RequestMapping("page/query")
	@ResponseBody Object pageQuery(TeacherHomeQueryVO vo) {
		try {
			Page<TeacherHomeView> page = new Page<>();
			Grid grid = new Grid();
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("CREATE_TIME", "desc"));
			sorts.add(new OrderBy("REPLY_TIME", "desc"));
			page.setSorts(sorts);
			page.setParams(vo);
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			List<TeacherHomeView> list = teacherHomeService.selectListByPage(page);
			grid.setRows(list);
			grid.setTotal(page.getTotalRecord());
			return grid;
		} catch (Exception e) {
			logger.error("---principalMailboxQuery error={}", e);
		}
		return null;
	}
	
	@RequestMapping("/publish")
	@ResponseBody Object publish(MailboxReqVO record) {
		try {
			AppUser appUser = getAppUser();
			record.setTermId(getCurrentTerm().getId());
			record.setParentId(appUser.getParent().getId());
			record.setSchoolId(appUser.getParent().getSchoolId());
			record.setSchoolName(appUser.getParent().getSchoolName());
			record.setStudentId(appUser.getParent().getStudentId());
			if(record.getIds() != null) {
				record.setIsAttach(1);
			}
			//teacherHomeService.addAndUpdate(record);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("----studentMailboxPrincipalCreate error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("/reply")
	@ResponseBody Object reply(TeacherHome record) {
		try {
			AppUser appUser = getAppUser();
			record.setTeacherId(appUser.getTeacher().getId());
			record.setTeacherName(appUser.getTeacher().getName());
			teacherHomeService.addAndUpdate(record);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("---studentMailboxPrincipalReply error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
}
